package com.wxzz.elearing.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.dto.SkillCenterCategoryDto;
import com.wxzz.elearing.comment.entity.SkillCenterCategory;
import com.wxzz.elearing.comment.mapper.SkillCenterCategoryMapper;
import com.wxzz.elearing.comment.service.ISkillCenterCategoryService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 技能沉淀/中心分类 服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-17
 */
@Service
public class SkillCenterCategoryServiceImpl extends ServiceImpl<SkillCenterCategoryMapper, SkillCenterCategory> implements ISkillCenterCategoryService {

    @Autowired
    SkillCenterCategoryMapper skillCenterCategoryMapper;

    @Override
    public ReturnVO<List<SkillCenterCategory>> listAllCategory(Long pid, Long enterpriseId, Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (pid == null) pid = new Long(0);
        queryWrapper.eq("enterprise_id", enterpriseId);
        queryWrapper.eq("pid", pid);
        queryWrapper.orderByDesc("sort");
        List<SkillCenterCategory> skillCenterCategories = skillCenterCategoryMapper.selectList(queryWrapper);
        SkillCenterCategory allCategory = new SkillCenterCategory();
        allCategory.setId(0L);
        allCategory.setCategoryName("全部");
        allCategory.setPid(0L);
        if (type == 1) {
            if (skillCenterCategories == null) {
                skillCenterCategories = new ArrayList<>();
                skillCenterCategories.add(allCategory);
            } else {
                skillCenterCategories.add(0, allCategory);
            }
        }
        return new ReturnVO("展示所有技能分类成功", skillCenterCategories);
    }

    @Override
    public ReturnVO saveCategory(SkillCenterCategory skillCenterCategory) {
        if (skillCenterCategory.getId() == null) {
            skillCenterCategoryMapper.insert(skillCenterCategory);
        } else {
            SkillCenterCategory category = skillCenterCategoryMapper.selectById(skillCenterCategory.getId());
            if (category == null) {
                throw new ServiceException("分类更新失败：分类不存在");
            }
            if (!category.getEnterpriseId().equals(skillCenterCategory.getEnterpriseId())) {
                throw new ServiceException("当前没有权限更改此分类");
            }
            skillCenterCategoryMapper.updateById(skillCenterCategory);
        }
        return new ReturnVO("新增技能分类成功", skillCenterCategory.getId());
    }

    @Override
    public ReturnVO deleteCategory(Long id, Long enterpriseId) {
        SkillCenterCategory skillCenterCategory = skillCenterCategoryMapper.selectById(id);
        if (skillCenterCategory == null) {
            throw new ServiceException("分类删除失败：分类不存在");
        }
        if (!enterpriseId.equals(skillCenterCategory.getEnterpriseId())) {
            throw new ServiceException("当前没有权限更改此分类");
        }
        // TODO: 删除子集
        skillCenterCategoryMapper.deleteById(id);
        return new ReturnVO("分类删除成功", id);
    }

    @Override
    public ReturnVO<JgPageVo<List<SkillCenterCategory>>> listCategory(SkillCenterCategoryDto skillCenterCategoryDto, Long enterpriseId, Integer type) {
        PageHelper.startPage(skillCenterCategoryDto.getCurPage(), skillCenterCategoryDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enterprise_id", enterpriseId);
        queryWrapper.eq("pid", 0);
        if (StringUtils.isNotBlank(skillCenterCategoryDto.getCategoryName())) {
            queryWrapper.like("category_name", skillCenterCategoryDto.getCategoryName() + "%");
        }
        queryWrapper.orderByDesc("sort");
        List<SkillCenterCategory> skillCenterCategories = skillCenterCategoryMapper.selectList(queryWrapper);

        SkillCenterCategory allCategory = new SkillCenterCategory();
        allCategory.setId(0L);
        allCategory.setCategoryName("全部");
        allCategory.setPid(0L);
        if (type == 1) {
            if (skillCenterCategories == null) {
                skillCenterCategories = new ArrayList<>();
                skillCenterCategories.add(allCategory);
            } else {
                skillCenterCategories.add(0, allCategory);
            }
        }

        //查询旗下直播数量
        for (SkillCenterCategory skillCenterCategory : skillCenterCategories) {
            Integer count = 0;
            // TODO: 查询一级分类下技能
            if (skillCenterCategory.getPid() == 0) {
//                count = platformLivetelecastMapper.selectCount(new LambdaQueryWrapper<PlatformLivetelecast>()
//                        .eq(PlatformLivetelecast::getLivetelecastCategoryIdFirst, livetelecastCategory.getId()));
                count = skillCenterCategoryMapper.selectCount(new LambdaQueryWrapper<SkillCenterCategory>()
                        .eq(SkillCenterCategory::getPid, skillCenterCategory.getId()));
            }
            skillCenterCategory.setSubNum(count);
        }
        return new ReturnVO("分类查询成功", new JgPageVo<>(new PageInfo(skillCenterCategories)));
    }
}
